<?php

namespace app\admin\controller\site;

use app\common\controller\Backend;
use app\common\model\Config as ConfigModel;

/**
 * 核心技术
 *
 * @icon fa fa-circle-o
 */
class Core extends Backend
{

    /**
     * Core模型对象
     * @var \app\admin\model\site\Core
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\site\Core;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function info()
    {
        if($this->request->isAjax()) {
            $core_name = input('row.core_name');
            $url = input('row.core_url');
            $intro = input('row.core_intro');
            $img = input('row.core_img');
            // 查询是否已存在数据
            $core = ConfigModel::where('name', 'core_name')->where('group', 'core')->find();
            if (!$core) {
                $core = new ConfigModel;
            }
            $core->name = 'core_name';
            $core->group = 'core';
            $core->title = 'core Name';
            $core->tip = '请填写显示名称';
            $core->type = 'string';
            $core->value = $core_name;
            $core->save();

            $core_url = ConfigModel::where('name', 'core_url')->where('group', 'core')->find();
            if (!$core_url) {
                $core_url = new ConfigModel;
            }
            $core_url->name = 'core_url';
            $core_url->group = 'core';
            $core_url->title = 'core Url';
            $core_url->tip = '请填写跳转链接';
            $core_url->type = 'string';
            $core_url->value = $url;
            $core_url->save();

            $core_intro = ConfigModel::where('name', 'core_intro')->where('group', 'core')->find();
            if (!$core_intro) {
                $core_intro = new ConfigModel;
            }
            $core_intro->name = 'core_intro';
            $core_intro->group = 'core';
            $core_intro->title = 'core Intro';
            $core_intro->tip = '请填写简介';
            $core_intro->type = 'string';
            $core_intro->value = $intro;
            $core_intro->save();

            $core_video = ConfigModel::where('name', 'core_img')->where('group', 'core')->find();
            if (!$core_video) {
                $core_video = new ConfigModel;
            }
            $core_video->name = 'core_img';
            $core_video->group = 'core';
            $core_video->title = 'core Img';
            $core_video->tip = '请上传介绍视频';
            $core_video->type = 'string';
            $core_video->value = $img;
            $core_video->save();
            $this->success();
        }

        $core = ConfigModel::where('group', 'core')->select();
        $data = [
            'core_name' => ['value' => '' ,'tip' => ''],
            'core_url' => ['value' => '' ,'tip' => ''],
            'core_intro' => ['value' => '' ,'tip' => ''],
            'core_img' => ['value' => '/assets/img/default.png' ,'tip' => ''],
        ];
        if($core){
            foreach ($core as $item){
                $data[$item['name']] = [
                    'value' => $item['value'],
                    'tip' => $item['tip'],
                ];
            }

        }
        $this->view->assign('data', $data);
        return $this->fetch('info');
    }

}
